package com.jacobson.core.framework.delegation

import java.lang.ref.WeakReference
import kotlin.reflect.KProperty

/**
 * ================================================
 *  弱引用委托类型
 *  @author Wucz on 2018/7/27 8:42
 * ================================================
 */

class WeakRef<T : Any>(initializer: () -> T?) {
    private var weakReference = WeakReference(initializer())

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T? = weakReference.get()
    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
        weakReference = WeakReference(value)
    }
}